var vm = new Vue({
    el: '#rrapp',
    data: {
        erpOrderProduction: [],
        sellerInfoList: [],      //商户列表
        costFormVisible: false,   // 成本录入显示
        currProductionIds: [],    //批量付款操作Ids
        currProductionId: '',       //作废当前productionId
        totalPrice: 0,//合计金额
        page: {currentPage: 0, pageSizes: [25, 50, 100], pageSize: 25, total: 0},
        q: {
            orderNo: '',
            financeStatus: '',
            beginCreatetime: '',
            endCreatetime: '',
            producerName: '',
            producerType: '',
            sellerId: '',
            onlineCash: '',
            beginCost: '',
            endCost: '',
            costStatus: '',
            productionStatus: ''
        },
        /* 录入成本*/
        costFrom: {
            cost: ''
        },
        costFormVisible: false,
        /* 付款*/
        payFrom: {
            productionId: '',
            financeRemark: '',
            financePaytime: '',
            cost: null
        },
        paymentFormVisible: false,  // 付款显示
        /* 付款重置按钮*/
        resetFrom(payFrom) {
            this.$refs[payFrom].resetFields();
        },
        /* 订单作废*/
        deadFrom: {
            productionId: '',
            financeRemark: ''
        },
        deadFormVisible: false,
        /* 重置*/
        resetFrom(deadFrom) {
            this.$refs[deadFrom].resetFields();
        }
    },
    created: function () { //加载数据
        this.loadData();
    },
    methods: {
        //首页数据
        loadData: function () {
            var params = this.q;
            params.page = this.page.currentPage;
            params.limit = this.page.pageSize;
            $.ajax({
                type: "POST",
                url: baseURL + "production/erporderproduction/productcostlist",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(params),
                success: function (r) {
                    if (r.code == 0) {
                        vm.erpOrderProduction = r.page.list;
                        vm.page.total = r.page.totalCount;
                        vm.sellerInfoList = r.userData.sellerInfoList;
                        vm.totalPrice = r.userData.totalPrice;
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        /** 搜索 */
        query: function () {
            vm.page.currentPage = 1;
            this.loadData();
        },
        /* 查询重置 */
        reset: function () {
            vm.q.orderNo = '';
            vm.q.productionStatus = '';
            vm.q.beginCreatetime = '';
            vm.q.endCreatetime = '';
            vm.q.producerName = '';
            vm.q.producerType = '';
            vm.q.sellerId = '';
            vm.q.onlineCash = '';
            vm.q.beginCost = '';
            vm.q.endCost = '';
            vm.q.costStatus = '';
            vm.q.financeStatus = '';
            vm.q.isquery = true;
            this.loadData();
        },
        //每页显示数据量变更
        handleSizeChange: function (val) {
            this.page.pageSize = val;
            this.loadData();
        },
        //当前页数据
        handleCurrentChange: function (val) {
            this.page.currentPage = val;
            this.loadData();
        },
        /* 获取当前日期 */
        dateFormat: function (date) {
            var year = date.getFullYear();
            var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
            var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
            var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
            var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
            return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
        },
        /*录入成本*/
        enteringCost: function (index, row) {
            var rowData = JSON.parse(JSON.stringify(row));
            vm.costFrom = Object.assign({}, rowData);
            this.costFormVisible = true;
        },
        costFormConfrim(costFrom) {
            var _this = this;
            var postData = {
                productionId: vm.costFrom.productionId,
                cost: vm.costFrom.cost
            };
            this.$refs[costFrom].validate(function (valid) {
                if (valid) {
                    //输入为负数，不写入数据库
                    if (vm.costFrom.cost <= 0) {
                        _this.$message.error('请输入大于0的数字');
                        return false;
                    }
                    $.ajax({
                        type: "POST",
                        url: baseURL + "production/erporderproduction/updatecost",
                        contentType: "application/json",
                        data: JSON.stringify(postData),
                        success: function (r) {
                            vm.costFormVisible = false;
                            if (r.code === 0) {
                                _this.$message({
                                    type: 'success',
                                    message: '操作成功!'
                                });
                                setTimeout(function () {
                                    vm.loadData();
                                }, 500);
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                } else {
                    console.log('error primary!!');
                    return false;
                }
            });
        },

        /*生产成本
         * 审核
         */
        Trial: function (index, row) {
            var _this = this;
            var rowData = JSON.parse(JSON.stringify(row));
            var productionId = rowData.productionId;
            var cost = rowData.cost;
            if (cost <= 0) {
                _this.$message.error('请录入成本再审核！');
                return false;
            }
            this.$confirm('确认审核通过?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                $.ajax({
                    type: "GET",
                    url: baseURL + "production/erporderproduction/toExamine",
                    contentType: "application/json",
                    data: {'productionId': productionId},
                    success: function (r) {
                        if (r.code === 0) {
                            _this.$message({
                                type: 'success',
                                message: '操作成功!'
                            }, 200);
                            setTimeout(function () {
                                vm.loadData();
                            }, 500)

                        } else {
                            alert(r.msg);
                        }
                    }
                });

            }).catch(function () {
                _this.$message({
                    type: 'info',
                    message: '已取消审核'
                });
            });
        },
        /* 复审 */
        review: function (index, row) {
            var _this = this;
            var rowData = JSON.parse(JSON.stringify(row));
            var productionId = rowData.productionId;
            this.$confirm('是否确认复审?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                callback: function (f) {
                    if (f == 'confirm') {
                        $.ajax({
                            type: "GET",
                            url: baseURL + "production/erporderproduction/review?productionId=" + productionId,
                            contentType: "application/json",
                            data: null,
                            success: function (r) {
                                if (r.code === 0) {
                                    _this.$message({
                                        type: 'success',
                                        message: '操作成功!'
                                    }, 200);
                                    setTimeout(function () {
                                        vm.loadData();
                                    }, 500)
                                } else {
                                    alert(r.msg);
                                }
                            }
                        });
                    }
                }
            })
        },
        /* 批量审核 */
        batchAudit: function () {
            var selected = this.$refs.multipleTable.selection;
            var productionIds = [];
            var _this = this;
            for (var index in selected) {
                productionIds.push(selected[index].productionId);
            }
            if (productionIds.length == 0) {
                _this.$message.error('最少选择一条数据 !');
                return false;
            }
            this.$confirm('是否确认批量审核, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                callback: function (f) {
                    if (f == 'confirm') {
                        var postData = productionIds.join(',');
                        $.ajax({
                            type: "GET",
                            url: baseURL + "production/erporderproduction/toBatchExamine?productionIds=" + postData,
                            contentType: "application/json",
                            data: null,
                            success: function (r) {
                                if (r.code === 0) {
                                    _this.$message({
                                        type: 'success',
                                        message: '操作成功!'
                                    }, 200);
                                    setTimeout(function () {
                                        vm.loadData();
                                    }, 500)
                                } else {
                                    alert(r.msg);
                                }
                            }
                        });
                    }
                }
            })

        },

        /* 开启 作废窗口*/
        dead: function (index, row) {
            var rowData = JSON.parse(JSON.stringify(row));
            vm.currProductionId = rowData.productionId;
            this.deadFormVisible = true;
        },

        /*生产成本
         * 订单作废
         */
        deadFormConfrim(deadFrom) {
            var _this = this;
            var postData = {
                'productionId': vm.currProductionId,
                'financeRemark': vm.deadFrom.financeRemark
            };
            this.$refs[deadFrom].validate(function (valid) {
                if (valid) {
                    $.ajax({
                        type: "POST",
                        url: baseURL + "production/erporderproduction/toDead",
                        contentType: "application/json",
                        data: JSON.stringify(postData),
                        success: function (r) {
                            if (r.code === 0) {
                                vm.deadFormVisible = false;
                                _this.$message({
                                    type: 'success',
                                    message: '操作成功!'
                                }, 200);
                                setTimeout(function () {
                                    vm.loadData();
                                }, 500)
                            } else {
                                alert(r.msg);
                            }
                        }
                    });

                } else {
                    console.log('error primary!!');
                    return false;
                }
            });
        },
        /*生产成本
         *
         * 订单撤回待审
         */
        withdraw: function (index, row) {
            var _this = this;
            var rowData = JSON.parse(JSON.stringify(row));
            var productionId = rowData.productionId;
            this.$confirm('确认订单撤回待审?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                $.ajax({
                    type: "GET",
                    url: baseURL + "production/erporderproduction/toWithdraw",
                    contentType: "application/json",
                    data: {'productionId': productionId},
                    success: function (r) {
                        //alert("操作成功!");
                        if (r.code === 0) {
                            _this.$message({
                                type: 'success',
                                message: '操作成功!'
                            }, 200);
                            setTimeout(function () {
                                vm.loadData();
                            }, 500);
                        } else {
                            alert(r.msg);
                        }
                    }
                });

            }).catch(function () {
                _this.$message({
                    type: 'info',
                    message: '已取消撤回操作'
                });
            });
        },

        /*
  * 生产成本
  * 订单付款
  */
        payment: function (index, row) {
            var rowData = JSON.parse(JSON.stringify(row));
            vm.payFrom.productionId = rowData.productionId;
            vm.payFrom.cost = rowData.cost;
            this.paymentFormVisible = true;
        },
        /**
         *  批量付款-总成本
         */
        batchPayment: function () {
            var selected = this.$refs.multipleTable.selection;
            var productionIds = [];
            var _this = this;
            for (var index in selected) {
                productionIds.push(selected[index].productionId);
            }
            if (productionIds.length == 0) {
                _this.$message.error('最少选择一条数据 !');
                return false;
            }
            vm.currProductionIds = productionIds;
            $.ajax({
                type: "POST",
                url: baseURL + "production/erporderproduction/productionCostCount",
                contentType: "application/json",
                data: JSON.stringify(vm.currProductionIds),
                success: function (r) {
                    if (r.code == 0) {
                        vm.payFrom.cost = r.totalCost;
                        vm.paymentFormVisible = true;
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },

        /**
         * 批量录入
         */
        batchEnteringCost: function () {
            var selected = this.$refs.multipleTable.selection;
            var productionIds = [];
            var _this = this;
            for (var index in selected) {
                var item = selected[index];
                var costStatus = item.costStatus;
                if (!costStatus) {
                    productionIds.push(item.productionId);
                }
            }
            if (productionIds.length == 0) {
                _this.$message.error('最少选择一条  未录入  的数据 !');
                return false;
            }

            this.$confirm('是否确认批量录入?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                callback: function (f) {
                    if (f == 'confirm') {
                        $.ajax({
                            type: "POST",
                            url: baseURL + "production/erporderproduction/batchEnteringCost",
                            contentType: "application/json",
                            data: JSON.stringify(productionIds),
                            success: function (r) {
                                if (r.code == 0) {
                                    _this.query();
                                } else {
                                    alert(r.msg);
                                }
                            }
                        });
                    }
                }
            })
        },
        /*
         * 付款
         */
        paymentForm(payFrom) {
            var _this = this;
            var financePaytime = vm.payFrom.financePaytime;
            if (financePaytime == '') {
                _this.$message({
                    type: 'error',
                    message: '请选择付款时间!'
                }, 200);
                return;
            }
            if (vm.currProductionIds.length > 0) {
                var productionId = vm.currProductionIds.join(',');
            } else {
                var productionId = vm.payFrom.productionId;
            }
            var postData = {
                'productionId': productionId,
                'financePaytime': vm.dateFormat(financePaytime),
                'financeRemark': vm.payFrom.financeRemark
            };
            this.$refs[payFrom].validate(function (valid) {
                if (valid) {
                    $.ajax({
                        type: "POST",
                        url: baseURL + "production/erporderproduction/toPayment",
                        contentType: "application/json",
                        data: JSON.stringify(postData),
                        success: function (r) {
                            if (r.code === 0) {
                                vm.paymentFormVisible = false;
                                _this.$message({
                                    type: 'success',
                                    message: '操作成功!'
                                }, 200);
                                setTimeout(function () {
                                    vm.loadData();
                                }, 500);
                            } else {
                                alert(r.msg);
                            }
                        }
                    });

                } else {
                    console.log('error primary!!');
                    return false;
                }
            });
        },
        //跳转订单详情页面
        productionCostDetail: function (orderNo) {
            window.Detail.detailData({'orderNo': orderNo});
        }
    }
});

//验证数字为大于0的非负整数
function validNumber(str) {
    var reg = /^\+?[1-9][0-9]*$/;
    if (!reg.test(str)) {
        return false;
    } else {
        return true;
    }
}